<template>
  <div class="container">
    <div class="leftSide">
      <!--header-->
      <div class="header">
        <div class="userimg">
          <img src="../../assets/metaverse/images/profile2.png" class="cover">
        </div>
        <ul class="nav_icons">
          <!-- <li><img src="images/消息.png"></li>-->
          <li><img src="../../assets/metaverse/images/更多.png"></li>
        </ul>
      </div>
      <!--search-->
      <!--<div class="search_chat">
          <div>
              <input type="text" placeholder="搜索...">
              <img src="images/search.png">
          </div>
      </div>-->
      <!--ChatList-->
      <div class="chatlist">
        <div class="block active">
          <div class="imgbx">
            <img src="../../assets/metaverse/images/iconfont-fuwushichang.png" class="cover">
          </div>
          <div class="details">
            <div class="listHead">
              <h4>黄教授</h4>
              <!-- <p class="time">14:25</p>-->
            </div>
            <!-- <div class="message_p">
                 <p>什么时候可以停药啊？</p>
             </div>-->
          </div>
        </div>
      </div>

      <div class="chatlist">
        <div class="block unread">
          <div class="imgbx">
            <img src="../../assets/metaverse/images/iconfont-fuwushichang.png" class="cover">
          </div>
          <div class="details">
            <div class="listHead">
              <h4>权教授</h4>
              <!-- <p class="time">15:10</p>-->
            </div>
            <!-- <div class="message_p">
                 <p>天气太热了，怎么预防中暑</p>
                 <b>1</b>
             </div>-->
          </div>
        </div>
      </div>

      <div class="chatlist">
        <div class="block">
          <div class="imgbx">
            <img src="../../assets/metaverse/images/iconfont-fuwushichang.png" class="cover">
          </div>
          <div class="details">
            <div class="listHead">
              <h4>焦教授</h4>
              <!--  <p class="time">11:15</p>-->
            </div>
            <!-- <div class="message_p">
                 <p>有点饿</p>
             </div>-->
          </div>
        </div>
      </div>


    </div>

    <div class="centerSide">
      <div class="header" style="background: #FAFAFA">
        <div class="imgText">
          <div class="userimg">
            <img src="../../assets/metaverse/images/iconfont-fuwushichang.png" class="cover">
          </div>
          <h4>黄教授<br><!--<span>在线</span>--></h4>
        </div>
        <ul class="nav_icons">
          <li><img src="../../assets/metaverse/images/用户名.png">&nbsp;&nbsp;转人工客服</li>
        </ul>
      </div>
      <div class="imageStyle"></div>
      <!--问答区域 -->
      <div class="chatAnswer">
        <!--<div class="questionDiv">
          {{calcQuestion}}
        </div>-->
        <div class="answerDiv" v-html="answer">
        </div>
      </div>
      <!--chat input-->
      <div class="chatbox_input">
        <!-- <div class="instrument">
             <img src="images/表情.png" >
             <img src="images/语音.png">
         </div>-->
        <div>
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入"
            v-model="question">
          </el-input>
         <!-- <textarea type="text" placeholder="请输入" v-model="question"></textarea>-->
        </div>

      </div>
      <div class="sendDiv">
        <el-button type="primary" :loading="loading" v-if="loading">加载中</el-button>
        <el-button type="primary" @click="sendChat"  v-else>发送</el-button>
       <!-- <input type="button" value="发送" @click="sendChat">-->
        <select>
          <option></option>
          <option>发送历史病历</option>
          <option>发送历史诊断</option>
          <option>发送历史用药</option>
        </select>
      </div>
    </div>
    <div class="rightSide">
      <div class="header" style="background: #FAFAFA">
        <div class="imgText">
          <h4>聊天记录</h4>
        </div>
      </div>
      <!--chatbox-->
      <div class="chatBox">
        <!--<div class="message my_message">
          <p>你在干嘛呢？<br><span>10:15</span> </p>
        </div>
        <div class="message frnd_message">
          <p>在写好玩的东西啊<br><span>10:15</span> </p>
        </div>
        <div class="message frnd_message">
          <p>天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了天气太热了<br><span>10:15 </span> </p>
        </div>
        <div class="message my_message">
          <p>对啊 也不下雨<br><span>10:15</span> </p>
        </div>
        <div class="message my_message">
          <p>希望以后凉快点<br><span>10:15</span> </p>
        </div>-->
      </div>

    </div>
  </div>
</template>

<script>
  import {chat} from '@/api/metaverse'
    export default {
      name: "chat",
      data(){
          return {
            question: "",
            answer: "",
            questionBak: "",
            questionResult: [],
            loading: false
          }
      },
      methods:{
        sendChat(){
          this.loading = true
          console.log("发送数据了",this.question)
          let question = this.question
          this.question = "";
          /*
          let data = {question:question,history:[]}
          chat(data).then(resp=>{
            console.log(resp)
            console.log(resp.response)
            if(resp.response){
              let ans = resp.response.replace(/\n/g,"<br>")
              console.log(ans);
              this.answer = "答：" + ans
            }

          }).finally(()=>{
            this.loading = false
          })*/
          let data = {
            knowledge_base_id:"byyl",
            question:question,
            history:[]
          }
          this.sendMsg(data,ev=>{
            console.log(ev.data)
            if(ev.data.indexOf("question") == -1){
              this.answer += ev.data
            }
            if(ev.data.indexOf("question") !== -1){
              let d = JSON.parse(ev.data);

              if(d.flag == "start"){
                this.answer = this.answer +"<br>"+"问："+d.question+"<br> 答：";
              }
              if(d.flag == "end"){
                this.loading = false
              }
            }
          })
        }
      },
      created(){

      },
      computed: {
        calcAnswer(){
          if(this.answer){
            this.questionBak = this.answer.length == 0? "": "答："+this.answer;
          }
          return this.questionBak;
        },
        calcQuestion(){
          if(this.loading){  // 已经点击发送
            return this.questionBak;
          }
          this.questionBak = this.question.length == 0? "": "问："+this.question;
          return this.questionBak;
        }
      }
    }
</script>

<style scoped>
  @import url('https://fonts.googleapis.com css2?family-Open+Sans:wght@300;400;00;600;700&display=swap');
  *
  {
    margin: 0;padding: 0;
    box-sizing: border-box;
    font-family: 'Open Sans' ,sans-serif;
  }
  body
  {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(#009688 0%,#009688 130px,#d9dbd5 130px,#d9dbd5 100%)
  }
  .container
  {
    margin: auto;
    position: relative;
    width: 1396px;
    max-width: 100%;
    height: calc( 100vh - 40px);
    background:#fff;
    box-shadow: 0 1px 1px 0 rgba(0,0,0,0.06),0 2px 5px 0 rgba(0,0,0,0.06);
    display: flex;
  }
  .container .leftSide
  {
    position: relative;
    flex:20%;
    background:#D9D9D9;
    border-right: 1px solid rgba(0, 0, 0, 0.2);
  }
  .container .centerSide
  {
    position: relative;
    flex: 60%;
    background:white;
  }

  .container .rightSide
  {
    position: relative;
    flex: 20%;
    background:#FAFAFA;
  }

  .container .rightSide::before
  {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.06;
  }
  .header
  {
    position: relative;
    width: 100%;
    height: 60px;
    background: #D9D9D9;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
  }
  .userimg
  {
    position: relative;
    width: 40px;
    height: 40px;
    overflow: hidden;
    border-radius: 50%;
    cursor: pointer;
  }
  .cover
  {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .nav_icons
  {
    display: flex;

  }
  .nav_icons li
  {
    display: flex;
    list-style: none;
    cursor: pointer;
    /* color: #51585c;*/
    color: #4095E5;
    font-size: 1.1em;
    margin-left: 22px;
    font-weight: 500;
    letter-spacing: 2px;
  }
  .search_chat
  {
    position: relative;
    width: 100%;
    height: 50px;
    background: #f6f6f6;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 15px;
  }
  .search_chat div
  {
    width: 100%;
  }
  .search_chat div input
  {
    width:100%;
    outline: none;
    border: none;
    background: #fff;
    padding: 6px;
    height: 38px;
    border-radius: 30px;
    font-size: 14px;
    padding-left: 40px;
  }
  .search_chat div input::placeholder
  {
    color: #bbb;
  }
  .search_chat div img
  {
    position: absolute;
    left: 30px;
    top: 14px;
    font-size: 1.5em;
  }
  .chatlist
  {
    position: relative;
    height: calc(100%-110px);
    overflow-y: auto;
  }
  .chatlist .block
  {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
    padding: 15px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  }
  .chatlist .block.active{
    background: #ebebeb;
  }
  .chatlist .block:hover
  {
    background:#f5f5f5;
  }
  .chatlist .block .imgbx
  {
    position: relative;
    min-width: 45px;
    height: 45px;
    overflow: hidden;
    border-radius: 50%;
    margin-right: 10px;
  }
  .chatlist .block .details
  {
    position: relative;
    width: 100%;
  }
  .chatlist .block .details .listHead
  {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
  }
  .chatlist .block .details .listHead h4
  {
    font-size: 1.1em;
    font-weight: 600;
    color: #111;
  }
  .chatlist .block .details .listHead .time
  {
    font-size: 0.75em;
    color: #aaa;
  }
  .chatlist .block .details .listHead .time
  {
    color: #111;
  }

  .message_p
  {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .message_p p
  {
    color: #aaa;
    display: -webkit-box;
    -webkit-line-break: 1;
    font-size: 0.9em;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .message_p b
  {
    background-color: red;
    color: #fff;
    min-width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.75em;
  }
  .imgText
  {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .imgText h4
  {
    font-weight: 500;
    line-height: 1.2em;
    margin-left: 15px;
  }
  .imgText h4 span
  {
    font-size: 0.8em;
    color: #555;
  }
  .chatBox
  {
    position: relative;
    width: 100%;
    height: calc(100%-120px);
    padding: 50px;
    overflow-y: auto;
  }
  .message
  {
    position: relative;
    display: flex;
    width: 100%;
    margin: 5px 0;
  }
  .message p
  {
    position: relative;
    right: 0;
    text-align: right;
    max-width: 65%;
    padding: 12px;
    background: #dcf8c6;
    border-radius: 10px;
    font-size: 0.9em;
  }
  .message p::before
  {
    content: '';
    position: absolute;
    top: 0;
    right: -12px;
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg,#dcf8c6 0%,#dcf8c6 50%,transparent 50%,transparent);
  }
  .message p span
  {
    display: block;
    margin-top: 5px;
    font-size: 0.85em;
    opacity: 0.5;
  }
  .my_message
  {
    justify-content: flex-end;
  }
  .frnd_message
  {
    justify-content: flex-start;
  }
  .frnd_message p
  {
    background: #fff;
    text-align: left;
  }
  .message.frnd_message p::before
  {
    content: '';
    position: absolute;
    top: 0;
    left: -12px;
    width: 20px;
    height: 20px;
    background: linear-gradient(225deg,#fff 0%,#fff 50%,transparent 50%,transparent);
  }
  .chatbox_input
  {
    position: absolute;
    width: 100%;
    height: 130px;
    /*background: rgba(143,144,147,0.7);*/
    background: #EDEDED;
    padding: 15px 10px;
    justify-content: space-between;
    align-items: center;
    bottom: 0px;
  }
  .chatbox_input textarea
  {
    position: relative;
    width: 95%;
    height: 100px;
    margin: 0 20px;
    padding: 10px 20px;
    border: none;
    outline: none;
    border-radius: 5px;
    font-size: 1em;
  }
  .imageStyle{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background: url("../../assets/metaverse/images/person.png");
    height: 85%;
  }
  .chatAnswer{
    position: absolute;
    background: rgba(145,147,156,0.7);
    width: 100%;
    max-height: 250px;
    bottom: 130px;
    color: white;
    overflow-y: auto;
  }
  .chatAnswer .answerDiv{
    margin: 0px 0px 0px 0px;
  }
  .sendDiv{
    position: absolute;
    width: 150px;
    height: 50px;
    right: 30px;
    bottom: 40px;
    /* border: 1px solid red;*/
    display: flex;
  }
  .sendDiv .el-button{
    height: 50px;
    width: 100px;
    border: none;
    background: #4095E5;
    color: white;
    font-size: 18px;
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    display: inline-block;
  }
  .sendDiv select{
    display: inline-block;
    width: 30px;
    height: 50px;
    background: #4095E5;
    color: white;
    border:none;
    margin-left: 3px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    text-align: center;
  }
</style>
